Relative Locators in Selenium
Write the TestClass in Sellenium
- Creating the Test Class:
- Create a new Java class in "src/test/java" under an appropriate package.
Write the Test Script in Sellenium
package asc;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;
import static org.openqa.selenium.support.locators.RelativeLocator.with;
import io.github.bonigarcia.wdm.WebDriverManager;
public class MT {
public static String browser="Chrome";
public static WebDriver driver;
public static void main(String[] args) {
// TODO Auto-generated method stub
if(browser.equals("Firefox"))
{
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
}
else if(browser.equals("Chrome")){
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
else if(browser.equals("Safari")){
WebDriverManager.safaridriver().setup();
driver = new SafariDriver();
}
else if(browser.equals("Edge")){
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
}
//WebDriverManager.chromedriver().setup();
//ChromeDriver driver = new ChromeDriver();
//FirefoxDriver driver = new FirefoxDriver();
//SafariDriver driver = new SafariDriver();
driver.get("https://www.saucedemo.com/v1/");
WebElement password = driver.findElement(By.id("password"));
//driver.findElement(with(By.tagName("input")).above(password)).sendKeys("testing3");
driver.findElement(with(By.tagName("input")).below(password)).click();
//driver.get("https://www.selenium.dev/documentation/webdriver/elements/locators/");
//driver.findElement(By.className("form_input")).sendKeys("testing");
//driver.findElement(By.cssSelector("#user-name")).sendKeys("testing1");
//driver.findElement(By.tagName("input")).sendKeys("testing2");
driver.findElement(By.linkText("Getting Started")).click();
driver.findElement(By.partialLinkText("Getting")).click();
//driver.findElement(By.id("user-name")).sendKeys("standard_user");
//driver.findElement(By.id("password")).sendKeys("secret_sauce");
//driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/div/form/input[3]")).click();
}
}
Relative Locators in Selenium:
Relative locators allow you to locate elements based on their position relative to other elements. The 'with' method from 'RelativeLocator' is used to create a relative locator. Here are some common relative locators:
- Above:
- (don't give space in the code) 'with (By .tagName ("input")) .above (referenceElement):' Finds the element above the reference element.
- Below:
- (don't give space in the code) 'with (By .tagName ("input")) .below (reference Element)': Finds the element below the reference element.
- ToLeftOf:
- (don't give space in the code) 'with (By .tagName ("input")) .toLeftOf (reference Element)': Finds the element to the left of the reference element.
- ToRightOf:
- (don't give space in the code) 'with (By .tagName ("input")) .toRightOf (reference Element) ': Finds the element to the right of the reference element.
- Near:
- ' with (By .tagName ("input")) .near (reference Element)': Finds the element near the reference element.